class Observer{
    constructor(data){
        this.walk(data)
    }
    walk(data){
        if(!data || typeof data !=="object"){
            return
        }
        Object.keys(data).forEach(key=>{
            this.defineReactive(data,key,data[key])
        })
    }

    defineReactive(data,key,val){
        this.walk(val)
        let dep=new Dep()
        let that=this
        Object.defineProperty(data,key,{
            enumerable:true,
            configurable:true,
            get(){
                //收集依赖
                Dep.target && dep.addSub(Dep.target)
                return val
            },
            set(value){
                if(val===value){
                    return
                }
                val=value
                that.walk(value)
                
                //发送通知
                dep.notify()
            }
        })
    }
}